﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vit.Common.Extensions
{
    public static class DataReaderExtensions
    {
        public static IEnumerable<T> ToObject<T>(this IDataReader reader) where T : class, new()
        {
            List<T> items = new List<T>();
            while (reader.Read())
            {
                T item = new T();
                for (int i = 0; i <= reader.FieldCount; i++)
                {
                    object val = reader[i];
                    string propName = reader.GetName(i);
                    item.SetPropertyValue(propName, val);
                }
                items.Add(item);
            }
            return items;
        }
    }
}
